<?php

/**
 * author: sebastien rannou - http://www.aimxhaisse.com
 *
 * description: parses json from curl query and put it into an object
 */


class	Perso
{
  var	$classes = array(1 =>		"Feca",
			 2 =>		"Osamodas",
			 3 =>		"Enutrof",
			 4 =>		"Sram",
			 5 =>		"Xelor",
			 6 =>		"Ecaflip",
			 7 =>		"Eniripsa",
			 8 =>		"Iop",
			 9 =>		"Cra",
			 10 =>		"Sadida",
			 11 =>		"Sacrieur",
			 12 =>		"Pandawa");

  var	$servers = array(1 =>		"Jiva",
			 2 =>		"Rushu",
			 3 =>		"Djaul",
			 4 =>		"Raval",
			 5 =>		"Hecate",
			 6 =>		"Sumens",
			 7 =>		"Menalt",
			 8 =>		"Rosal",
			 9 =>		"Maimane",
			 10 =>		"Silvosse",
			 11 =>		"Brumaire",
			 12 =>		"Pouchecot",
			 13 =>		"Silouate",
			 14 =>		"Domen",
			 15 =>		"Amayiro",
			 16 =>		"Rykke-Errel",
			 17 =>		"Hyrkul",
			 18 =>		"Helsephine",
			 19 =>		"Allister",
			 20 =>		"Otomai",
			 21 =>		"Lily",
			 23 =>		"Hel Munster",
			 24 =>		"Danathor",
			 25 =>		"Kuri",
			 26 =>		"Mylaise",
			 27 =>		"Goultard",
			 28 =>		"Ulette",
			 29 =>		"Vil Smiss",
			 30 =>		"Many",
			 31 =>		"Solar",
			 1001 =>	"Shika",
			 3001 =>	"Nehra",
			 4001 =>	"Alma",
			 4002 =>	"Aguabrial",
			 4003 =>	"Buhorado",
			 4004 =>	"Tenebres",
			 6001 =>	"Spiritia",
			 7001 =>	"Dark Vlad",
			 9001 =>	"Ereziah");

  var	$name;
  var	$level;
  var	$classe;
  var	$server;
  var	$id;
  var	$image_class;
  var	$image;
  var	$filename;

  const	font = "fonts/sketch.ttf";

  public function makeImage()
  {
    if (!$this->id)
       return;
    $this->image_class = imagecreatefromjpeg("dofus_images/" . strtolower($this->classe) . ".jpg");
    $this->image = imagecreatetruecolor(125, 170);
    if (!$this->image || !$this->image_class)
       return;
    imagefill($this->image, 0, 0, imagecolorallocate($this->image, 255, 255, 255));
    imagecopy($this->image, $this->image_class, 0, 10, 0, 0, 125, 139);
    $black_color = imagecolorallocate($this->image, 0, 0, 0);
    imagettftext($this->image, 10, 2, 5, 145, $black_color, self::font, $this->name);
    imagettftext($this->image, 8, -5, 5, 10, $black_color, self::font, $this->server);
    imagettftext($this->image, 12, -5, 50, 160, $black_color, self::font, "level " . $this->level);
    imagepng($this->image, $this->filename);
  }

  public function __construct($data)
  {
    $data = (array) $data;
    if (isset($data['na']))
       $this->name = $data['na'];
    if (isset($data['le']))
        $this->level = $data['le'];
    if (isset($data['br']))
        $this->classe = $this->classes[(int) $data['br']];
    if (isset($data['hs']))
        $this->server = $this->servers[(int) $data['hs']];
    if (isset($data['id']))
        $this->id = $data['id'];
    if ($this->id)
        $this->filename = "images/player_" . $this->id . ".png";
  }

  public function getInfos()
  {
    $infos = "informations sur " . $this->name . " - ";
    $infos .= "level " . $this->level . " (" . $this->classe . ")";
    $infos .= " - " . $this->server;
    return $infos;
  }
}
